package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.common.web.BaseController;

import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobLink;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobMes;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobWork;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobLinkService;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobMesService;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobWorkService;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping(value = "/api/schoolSource/jobmes")
public class JobMesApiController extends BaseController{
    @Autowired
    private JobLinkService jobLinkService;
    @Autowired
    private JobWorkService jobWorkService;
    @RequestMapping(value = "jobList")
    @ResponseBody  //获取就业信息列表
    public Map<String, Object> getJob(JobWork jobWork, HttpServletRequest request, HttpServletResponse response) {
        jobWork.setJobStates("1");
        Page<JobWork> page = jobWorkService.findPage(new Page<JobWork>(request, response), jobWork);
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        map.put("page", page.getList());
        map.put("pageNo", page.getPageNo());
        map.put("count", page.getCount());
        map.put("totalPage", page.getTotalPage());
        return map;
    }
    @RequestMapping(value = "findById")
    @ResponseBody  //获取就业信息的详情
    public Map<String, Object> findById(JobWork jobWork, HttpServletRequest request, HttpServletResponse response) {
        JobWork getjobwork = jobWorkService.get(jobWork);
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        map.put("page", getjobwork);
        return map;
    }
    @RequestMapping(value = "enlist")
    @ResponseBody  //就业信息报名
    public Map<String, Object> getList(JobLink jobLink, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal(); //用户是否掉线
        if(principal !=null) {
            jobLink.setStuId(principal.toString());
            List<JobLink> findjobLink = jobLinkService.findList(jobLink);//判断用户是否已经报名
            if(findjobLink.isEmpty()){
                if(StringUtils.isNotBlank(jobLink.getLinkJob())){
                    jobLinkService.save(jobLink);
                    map.put("message","报名成功");
                    map.put("success",true);
                    return map;
                } else {
                    map.put("message","没有上传报名编号");
                    map.put("success",false);
                    return map;
                }
            } else {
                map.put("message","用户已经报名");
                map.put("success",false);
                return map;
            }
        } else {
            map.put("message","用户掉线");
            map.put("success",false);
            return map;
        }
    }

}
